﻿@inherits _BaseView
@implements IDisposable

<MudRTLProvider RightToLeft="@SharedLayoutSettings.IsRTL">
    <MudDialog>
        <DialogContent>
            @if (!IsLoaded)
            {
                <FormOverlay />
            }
            else
            {
                <MudCard Class="mt-2">
                    <MudCardHeader>
                        <MudText Typo="Typo.button" Color="Color.Primary">
                            <MudText>@DashboardResource.StringConfiguration</MudText>
                        </MudText>
                    </MudCardHeader>
                    <MudDivider DividerType="DividerType.FullWidth" />
                    <MudCardContent>
                        <MudTabs Outlined Position="Position.Start">
                             <MudTabPanel Text="@DashboardResource.StringStorage" Icon="@Icons.Material.Filled.Storage">
                                 <MudGrid>
                                     <MudItem md="4"></MudItem>
                                     <MudItem md="4">
                                         <MudButton OnClick="ClearCache" Disabled="@IsStorageButtonDisable"
                                                    Variant="Variant.Filled" Color="Color.Primary">
                                             @DashboardResource.StringClearCache
                                         </MudButton>
                                     </MudItem>
                                     <MudItem md="4"></MudItem>
                                </MudGrid>
                            </MudTabPanel>
                        </MudTabs>
                    </MudCardContent>
                </MudCard>
                <MudPaper Class="pa-3 my-2 ActionColor" Elevation="3" Outlined="true" Square="true">
                    <MudStack Spacing="1" Row="true">
                        <MudButton OnClick="Close" Variant="Variant.Filled" Color="Color.Primary" StartIcon="@Icons.Material.Filled.FreeCancellation" IconColor="Color.Surface">@DashboardResource.StringCancel</MudButton>
                    </MudStack>
                </MudPaper>
            }
        </DialogContent>
    </MudDialog>
</MudRTLProvider>

@code {

    [CascadingParameter]
    public MudDialogInstance MudDialog { get; set; }

    private void Close() => MudDialog.Close();

    // *****************************************************

    private List<string> Storages = new();
    private bool IsStorageButtonDisable = false;

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        try
        {
            await CheckAvailableStorage();
        }
        catch (Exception ex)
        {
            Snackbar.Add($"{DashboardResource.StringError} : {ex.ToString()}", Severity.Error);
        }
        finally
        {
            IsLoaded = true;
        }
    }

    private async Task ClearCache()
    {
        try
        {
            await LocalStorage.ClearAsync();
            await CheckAvailableStorage();
        }
        catch (Exception ex)
        {
            Snackbar.Add($"{DashboardResource.StringError} : {ex.ToString()}", Severity.Error);
        }
    }

    private async Task CheckAvailableStorage()
    {
        try
        {
            Storages = await LocalStorage.KeysAsync();

            if (!Storages.Where(x => x.Contains("ConfigurationStorage") || x.Contains("CultureStorage")).Any())
            {
                IsStorageButtonDisable = true;
            }
        }
        catch (Exception ex)
        {
            Snackbar.Add($"{DashboardResource.StringError} : {ex.ToString()}", Severity.Error);
        }
    }

    public async void Dispose() => await Dom.DisposeAsync();

}
